<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Hd_game_customer extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('hdcustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_hd_game_gift;
        $data['game_gift'] = array4js($g_hd_game_gift);

        $this->load->view('admin/hd_game_customer',$data);
    }

    public function getCustomerList(){

        $assist['where'] = getFilter($this->input->post(),hdcustomermodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),hdcustomermodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),hdcustomermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        if(isset($assist['where']['hd_game_customer.gift_type']) && $assist['where']['hd_game_customer.gift_type'] < 0) {
            unset($assist['where']['hd_game_customer.gift_type']);
            $assist['where']['hd_game_customer.gift_type >'] ='0';
        }
        list($list,$count) = $this->m_customer->getCustomerListSection($assist);

        foreach ($list as $key => $value) {
            $list[$key]['nickname'] = urldecode($value['nickname']);
        }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function getLotteryHistoryList() {
        $assist['where'] = getFilter($this->input->post(),Hdgamelotteryhistorymodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Hdgamelotteryhistorymodel::$likeField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_customer->getLotteryHistoryListSection($assist);

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function exportCustomerList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '2048M');


        $assist['where'] = getFilter($this->input->get(),hdcustomermodel::$searchField);
        $assist['like'] = getFilter($this->input->get(),hdcustomermodel::$likeField);
        $assist['sort'] = getSort($this->input->get(),hdcustomermodel::$sortField);

        if(isset($assist['where']['hd_game_customer.gift_type']) && $assist['where']['hd_game_customer.gift_type'] < 0) {
            unset($assist['where']['hd_game_customer.gift_type']);
            $assist['where']['hd_game_customer.gift_type >'] ='0';
        }

        $this->load->library('excel');
        $this->excel->init('北京现代游戏用户_'.date('YmdH').'.xls');
        $this->excel->addSheet('北京现代游戏用户');
        $this->excel->addTitle(array(
            array('content'=>'ID'),
            array('content'=>'奖品'),
            array('content'=>'昵称'),
            array('content'=>'openid'),
            array('content'=>'性别'),
            array('content'=>'省份'),
            array('content'=>'城市'),
            array('content'=>'国家'),
            array('content'=>'创建时间'),
            array('content'=>'抽奖总数'),
            array('content'=>'收货人姓名'),
            array('content'=>'收货人手机'),
            array('content'=>'收货人地址'),
        ));
        
        list($list,$count) = $this->m_customer->getCustomerListSection($assist);


        global $g_hd_game_gift;

        $sex_array = array(
            0 => '未知',
            1 => '男',
            2 => '女',
        );
        foreach ($list as $key => $value) {
            $value['nickname'] = urldecode($value['nickname']);
            $gift_info = array();

            $this->excel->addRow(array(
                array('content' => $value['customer_id']),
                array('content' => id2name($value['gift_type'],$g_hd_game_gift)),
                array('content' => $value['nickname']),
                array('content' => $value['openid']),
                array('content' => $sex_array[$value['sex']]),
                array('content' => $value['province']),
                array('content' => $value['city']),
                array('content' => $value['country']),
                array('content' => $value['customer_time_create']),
                array('content' => $value['lottery_times']),
                array('content' => $value['lottery_times']),
                array('content' => $value['customer_name']),
                array('content' => $value['customer_phone']),
                array('content' => $value['customer_address']),
            ));
        }

        $this->excel->freezePanes(1,0);
        $this->excel->generate();
    }

    public function disableCustomer() {
        $this->validate->setRule('customer_id','integer');
        $this->validate->setRule('customer_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['customer_status'] = STATUS_DELETE;
        $this->m_customer->editCustomer($info);

        $this->response->setSuccess();
        $this->response->setDetail('更新成功!');
        $this->response->responseExit();
    }

    public function enableCustomer() {
        $this->validate->setRule('customer_id','integer');
        $this->validate->setRule('customer_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['customer_status'] = STATUS_DISABLE;
        $this->m_customer->editCustomer($info);

        $this->response->setSuccess();
        $this->response->setDetail('更新成功!');
        $this->response->responseExit();
    }
}
/* End of file customer.php */
/* Location: ./application/controllers/customer.php */